package com.ccp.dev.system.service;

import com.ccp.dev.core.basic.base.BaseService;
import com.ccp.dev.core.basic.util.ContextUtil;
import com.ccp.dev.core.util.StringPool;
import com.ccp.dev.core.util.StringUtil;
import com.ccp.dev.system.consts.FileManagementContants;
import com.ccp.dev.system.dao.FileManagementSquaredDao;
import com.ccp.dev.system.model.FileManagementSquared;
import com.ccp.dev.system.model.SysUser;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;


@Service
public class FileManagementSquaredService extends BaseService<FileManagementSquared> {
    @Resource
    FileManagementSquaredDao fileManagementSquaredDao;
    @Resource
    FileManagementService fileManagementService;

    /**
     * 获取九宫格文件列表 List页面
     * @param parentType 1物理树 2逻辑树
     * @param parentId 父级文件ID
     * @param fileName 文件名
     * @param basePath 基础路径
     */
    public List<FileManagementSquared> getFilesByParam(String parentType, String parentId, String fileName, String basePath){
        SysUser sysUser = (SysUser) ContextUtil.getCurrentUser(); // 获取当前用户
        //获取当前用户文件密级
        int[] fileSecretLevels = FileManagementContants.getFileSecretByUserSecret(sysUser.getPrivacyLevel());
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("parentId",parentId);
        if(StringUtil.isNotEmpty(fileName)){
            fileName = StringPool.PERCENT + fileName + StringPool.PERCENT;
            params.put("fileName",fileName);
        }
        params.put("parentType",parentType);
        params.put("fileSecretLevels",fileSecretLevels);
        List<FileManagementSquared> fileList = fileManagementSquaredDao.getFilesByParam(params);
        List<FileManagementSquared> retList = new ArrayList<>();
        List<Map<String, Object>> viewList = fileManagementService.getFolderTree(Integer.parseInt(parentType), basePath);
        //过滤到没有权限的目录节点
        for (FileManagementSquared item : fileList) {
            if (item.getFileType() == FileManagementContants.FILE_TYPE_FILE) {
                retList.add(item);
            } else {
                for (Map<String, Object> map : viewList) {
                    if (item.getId().equals(map.get("id").toString())) {
                        retList.add(item);
                    }
                }
            }
        }
        return retList;
    }

    /**
     * 获取九宫格文件列表 菜单界面
     * @param parentType 1物理树 2逻辑树
     * @param parentId 父级文件ID
     * @param fileName 文件名
     * @param basePath 基础路径
     * @param topFolderId 菜单文件夹ID
     */
    public List<FileManagementSquared> getFilesByParam(String parentType, String parentId, String fileName, String basePath, String topFolderId){
        SysUser sysUser = (SysUser) ContextUtil.getCurrentUser(); // 获取当前用户
        //获取当前用户文件密级
        int[] fileSecretLevels = FileManagementContants.getFileSecretByUserSecret(sysUser.getPrivacyLevel());
        List<FileManagementSquared> fileList = new ArrayList<>();
        //搜索情况
        if (fileName!=null){
            findFilesInFolder(parentType, topFolderId, fileSecretLevels,fileList);
            //匹配查询关键字
            if (fileList.size()>0&&fileName!=null){
                Iterator iterator = fileList.iterator();
                while (iterator.hasNext()){
                    FileManagementSquared next = (FileManagementSquared)iterator.next();
                    if (!next.getFileName().contains(fileName)){
                        iterator.remove();
                    }
                }
            }
        }else {
            Map<String, Object> params = new HashMap<String, Object>();
            params.put("parentId",parentId);
            params.put("parentType",parentType);
            params.put("fileSecretLevels",fileSecretLevels);
            //非搜索情况
            fileList = fileManagementSquaredDao.getFilesByParam(params);
        }
        List<FileManagementSquared> retList = new ArrayList<>();
        List<Map<String, Object>> viewList = fileManagementService.getFolderTree(Integer.parseInt(parentType), basePath);
        //过滤到没有权限的目录节点
        for (FileManagementSquared item : fileList) {
            if (item.getFileType() == FileManagementContants.FILE_TYPE_FILE) {
                retList.add(item);
            } else {
                for (Map<String, Object> map : viewList) {
                    if (item.getId().equals(map.get("id").toString())) {
                        retList.add(item);
                    }
                }
            }
        }
        return retList;
    }

    private void findFilesInFolder(String parentType, String parentId, int[] fileSecretLevels, List fileList){
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("parentId",parentId);
        params.put("parentType",parentType);
        params.put("fileSecretLevels",fileSecretLevels);
        List<FileManagementSquared> files = fileManagementSquaredDao.getFilesByParam(params);
        if (files!=null&&files.size()>0){
            fileList.addAll(files);
            for (FileManagementSquared fileManagementSquared:files){
                //若是文件夹则搜索下层目录
                if (fileManagementSquared.getFileType()==1){
                    findFilesInFolder(parentType,fileManagementSquared.getId(),fileSecretLevels,fileList);
                }
            }
        }
    }
}
